/*
@STI LICENSE
(C) 2006-2008 Universit�t Innsbruck.

This file is part of myOntology.

myOntologyis free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

myOntologyis distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with myOntology.  If not, see <http://www.gnu.org/licenses/>.

@STI LICENSE */
package view.HTMLUIcomponents;

import java.util.ArrayList;

import model.DHTMLConstants;
import model.Labels;
import model.Vocabulary;

import org.apache.commons.lang.StringEscapeUtils;
import org.openrdf.model.URI;

import controller.HelperFunctions;
import controller.elementMapper.Mapper;
import controller.elementMapper.VersioningMapper;
import controller.sesameDataConnector.AbstractDataConnector;


public class Label extends AbstractComponent {
	

	private static final String CSSIDFIELD = "divlabel";
	private static final String CSSIDFIELD_TITLE = "divlabeltitle";
	public static final String CSSIDCONTENT = CSSIDFIELD;
	public static final String CSSIDCONTENT_TITLE = CSSIDFIELD_TITLE;
	public static final String NAMEFORMELEMENT = "labelEntry";

	

	public static String getEmptyFieldContainer() {

		String ret = AbstractComponent.getEmptyFieldContainer( CSSIDFIELD );
		
		return ret;
	}
	
	
	/**
	 * Returns the field in view mode.
	 * @param id
	 * @param entry
	 * @return
	 */
	public static String getFieldInEditMode( URI uri, String entry, URI type, ArrayList<Exception> exceptions, boolean showNavigation ) throws Exception {
		
		String ret = "";

		String content = getFormInEditMode( uri, entry, type, exceptions, showNavigation );

		ret += getFieldContainer( content, CSSIDFIELD );

		//ret = "<div id=\"" + CSSIDFIELD + "\">" + ret + "</div>";
		
		return ret;
	}
	


	public static String getTitleFieldInViewMode( URI uri, URI type, boolean showNavigation ) throws Exception {

		String ret = getTitleForm(uri, type, showNavigation);
		
		ret = "<div id=\"" + CSSIDFIELD_TITLE + "\">" + ret + "</div>";
		
		return ret;
	}	
	public static String getTitleFieldInEditMode( URI uri, URI type, boolean showNavigation ) throws Exception {

		String ret = getTitleForm(uri, type, showNavigation);
		
		ret = "<div id=\"" + CSSIDFIELD_TITLE + "\">" + ret + "</div>";
		
		return ret;
	}
	
	/**
	 * 
	 * @param id
	 * @param type
	 * @param showNavigation	true: show link to edit;
	 * @return
	 * @throws Exception
	 */
	public static String getTitleForm( URI uri, URI type, boolean showNavigation ) throws Exception {
		
		String ret = "";
		String linkToEdit = "";
		
		String h1class = "";
		if( type!=null) {
			if( type.equals(Vocabulary.CLASS))
				h1class = "ct";
			else if( type.equals(Vocabulary.DATATYPEPROPERTY) || type.equals(Vocabulary.OBJECTPROPERTY))
				h1class = "pt";
			else if( type.equals(Vocabulary.ONTOLOGYMODULE))
				h1class = "ot";
		}

		ret += getHiddenIdFields( uri );
		
		if( uri!=null ) {

			String entry = Mapper.selectLabel(uri, Vocabulary.ENGLISH);	

			if( showNavigation )
				linkToEdit = getLinkToEditfield(CSSIDFIELD, Mapper.selectHasVersionDateTimeString(uri), true );
			
			model.Label lab = Labels.getLabel( type );			
			
			ret += lab.getNameSingular();
			
			String note = "";
			if( !lab.getComment().equals( "" ) ) {
				note += lab.getComment() + "<br/>";
			}
			if( !lab.getSynonyms().equals( "" ) ) {
				note += "Synonyms for this type: " + lab.getSynonyms();
			}			
			ret += controller.HelperFunctions.getNotification( note );
			

			String deprectatedInfo = "\"Deprecated\" means, that there exist improved elements, which should be used instead.";
			if( !Mapper.isDeprecated(uri)) {

				ret += " " + linkToEdit + "<strong>" + StringEscapeUtils.escapeHtml( entry ) + "</strong>\n";
				
				ret = 
					"<h1 class=\"" + h1class + "\">" + ret + "";
				
				if( showNavigation ) { 
					ret += "<span id=\"deprediv\" onmouseover=\"at_attach('deprediv', 'deprechild', 'hover', 'y', 'pointer'); this.onmouseover();\">" +
						"<img src=\"skins/default/images/wrench.png\" alt=\"(not deprecated)\" />" +
						"</span>\n" +
						"<div id=\"deprechild\" style=\"position: absolute; visibility: hidden;\" class=\"sample_attach\">" +
						"<a href=\"javascript:markAsDepricated('deprechild','"+ DHTMLConstants.MARKASDEPRICATED +"');\">mark as deprecated" + "</a>" + HelperFunctions.getNotification( deprectatedInfo ) +
						"</div>\n";
				}
				
				ret += " </h1>\n";		
			}
			else {

				ret += " " + linkToEdit + "<strong class=\"deprecated\"> " + StringEscapeUtils.escapeHtml( entry ) + " </strong>\n";
				
				ret = 
					"<h1 class=\"" + h1class + "\">" + ret + "";

				if( showNavigation ) { 
					ret += "<span id=\"deprediv\" onmouseover=\"at_attach('deprediv', 'deprechild', 'hover', 'y', 'pointer'); this.onmouseover();\">" +
						"<span style=\"color:#FF0000;font-size:12px;\">(deprectated)</span><img src=\"skins/default/images/wrench_orange.png\" alt=\"(deprecated)\" />" +
						"</span>\n" +
						"<div id=\"deprechild\" style=\"position: absolute; visibility: hidden;\" class=\"sample_attach\">" +
						"<a href=\"javascript:markAsNonDepricated('deprechild','"+ DHTMLConstants.MARKASNONDEPRICATED +"');\">mark non-deprecated" + "</a>" + HelperFunctions.getNotification( deprectatedInfo ) +
						"</div>\n";
				}
				
				ret += " </h1>\n";	
			}
					

			ret += getNewerVersionNote( VersioningMapper.getMostActualVersion( uri ) );
		}
		else {			

			model.Label lab = Labels.getLabel( type );			
			
			ret += "New " + lab.getNameSingular();
			
			String note = "";
			if( !lab.getComment().equals( "" ) ) {
				note += lab.getComment() + "<br/>\n";
			}
			if( !lab.getSynonyms().equals( "" ) ) {
				note += "Synonyms for this type: " + lab.getSynonyms();
			}			
			ret += " " + controller.HelperFunctions.getNotification( note );
			
			ret = "<h1 class=\"" + h1class + "\">" + ret + "</h1>\n";
		}

		
		return ret;
	}
	
	private static String getHiddenIdFields( URI uri ) {
		
		String ret = "";		
		String label = "";
		
		if( uri!=null )
			label = HelperFunctions.getUriForHref( uri );

		ret = "<input type=\"hidden\" name=\"elementuri\" value=\"" + label + "\" />";
		// ret += "<input type=\"hidden\" name=\"oldtimestamp\" value=\"" + Mapper.selectHasVersionDateTimeString(uri) + "\" />";
		
		return ret;
	}
	
	private static String getNewerVersionNote( URI uri ) throws Exception {
		
		String ret = "";
		
		ArrayList<URI> list = AbstractDataConnector.selectObjects( uri, Vocabulary.WASRENAMEDTO, null );

		if( list!=null && list.size() > 0 ) {
			ret += "Note that there might be a newer version of " + Mapper.selectLabel( uri, Vocabulary.ENGLISH ) + ": ";
		
			ret += HelperFunctions.getListOfElements(list, Vocabulary.ENGLISH);	
		}
		
		return ret;		
	}

	

	public static String getFormInEditMode( URI uri, String entry, URI type, ArrayList<Exception> exceptions, boolean showNavigation ) throws Exception {
		
		String ret = "";
		

		if( entry==null )
			entry = "";
		
		// ret = getTitleInViewMode( uri, type, false );
		

		String retinner = getTitleContainer( model.Labels.LABEL, null, true, false);
		
		String actualVersionContent = NOTHINGSELECTED;
		String actualTimestamp = Mapper.selectHasVersionDateTimeString(uri);
		
		if( uri!=null ) {
			String tmp = Mapper.selectLabel( uri, Vocabulary.ENGLISH );
			if( tmp!=null ) {
				actualVersionContent = StringEscapeUtils.escapeHtml( tmp );				
			}
		}
		
		String viewtype = null;
		
		if( type.equals(Vocabulary.CLASS) || type.equals(Vocabulary.ARCHIVEDCLASS) ) {
			
			viewtype = DHTMLConstants.CLASS_VIEWTYPE;			
		}
		else if( type.equals(Vocabulary.ONTOLOGYMODULE) || type.equals(Vocabulary.ARCHIVEDONTOLOGYMODULE) ) {

			viewtype = DHTMLConstants.ONTOLOGY_VIEWTYPE;	
		}	
		else if( type.equals(Vocabulary.DATATYPEPROPERTY) || type.equals(Vocabulary.OBJECTPROPERTY) || type.equals(Vocabulary.ARCHIVEDDATATYPEPROPERTY) || type.equals(Vocabulary.ARCHIVEDOBJECTPROPERTY) ) {
			
			viewtype = DHTMLConstants.PROPERTY_VIEWTYPE;
		}		
		
		String content = getLabelField( entry, viewtype );	


		retinner += renderEditForm(
				Labels.LABEL,  
				content,
				exceptions, 
				actualVersionContent,
				actualTimestamp, // actualversionContent is the same as its entry; not the same e.g. at Tags because there actualverionContent would be linked 
				CSSIDFIELD, 
				NAMEFORMELEMENT, 
				showNavigation );

		/*if( uri!=null ) {
			String typename = Labels.getLabel(type).getNameSingular();
			
			String label = StringEscapeUtils.escapeHtml( entry );
			retinner += "Note that renaming the " + Labels.LABEL.getNameSingular() + " will" +
					"<ol>\n" +
					"	<li>create a new " + typename + " (with the actual contents; the original " + typename + " (" + label + ") will not be changed) and </li>\n" +
					"	<li>include the new " + typename + " everywhere, where " + label + " is used.</li>\n" +
					"</ol>\n";
		}
		*/

		//ret += "<div style=\"width:50%;float:left;\"><div style=\"margin-right:10px;\">" + retinner + "</div></div>";
		 ret += retinner;
			
	
		return ret;
	}
	
	

	private static String getLabelField( String entry, String viewtype ) {
		String ret = "";
		
		if( entry==null )
			entry = "";
		

		ret += "<div id=\"" + NAMEFORMELEMENT + "formaterrors\"></div>";
		
		ret += 
			"	<input autocomplete=\"off\" style=\"width:100%;\" tabindex=\"\" id=\"" + NAMEFORMELEMENT + "\" name=\"" + NAMEFORMELEMENT + "\" value=\"" + StringEscapeUtils.escapeHtml( entry ) + "\" type=\"text\" " +
					" onfocus=\"this.onkeyup(event);\" onkeyup=\"if( isCharacterInput(event) && validateLabelSetField(this,document.getElementById('" + NAMEFORMELEMENT + "_errors')) ) { checkForExistingSynonyms(this, " + model.DHTMLConstants.SYNONYMCHECK + ");" +
					"giveSimilarMatches('" + viewtype + "',this,'" + model.DHTMLConstants.TITLEEXISTS + "'); }\" />\n";

		if( !Labels.LABEL.getComment().equals("")) {
			ret +=
				"<div class=\"inputhint\">Note that a label is only of high value if everybody would label it equal!</div>";
		}
	
		// target for the suggestions (elemements with the letters of entry in the label).
		ret += 
			"	<div id=\"labelSuggest\"></div>\n";
		
		//show wordnet-entry for the label
		String fieldName = "slidebox1";
		
		//ret += "<a id=\"toggle\" href=\"javascript:void(0)\" name=\"toggle\">Show what WordNet suggests.</a>";
		
		ret += "<div id=\"" + fieldName + "\"></div>";	

		
		
		/*
		ret += 
			"<script type=\"text/javascript\">" +
			"	window.addEvent('domready', function(){" +
			"	var mySlide1 = new Fx.Slide('" + fieldName + "');" +
			"	mySlide1.hide();" +
			"	$('toggle').addEvent('click', function(e){" +
			"			e = new Event(e);" +
			"			mySlide1.toggle();" +
			"			e.stop();" +
			"			});" +
			"	});" +
			"</script>";
		*/
		
		return ret;
	}

}
